package de.lianowar.whereismymoney.fragment;


import java.util.ArrayList;

import de.lianowar.whereismymoney.R;
import de.lianowar.whereismymoney.adapter.AccountListAdapter;
import de.lianowar.whereismymoney.adapter.DetailListAdapter;
import de.lianowar.whereismymoney.sql.DatabaseHandler;
import de.lianowar.whereismymoney.sql.data.Account;
import de.lianowar.whereismymoney.sql.data.MoneyAmount;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class Fra_Details extends Fragment
{
	private DatabaseHandler dbHandler = null;
	private Button refresh;
	private TextView debug;
	
	private DetailListAdapter detailsAdpa;
	private ArrayList<MoneyAmount> mData;
	private Spinner s;
	private AccountListAdapter adapter;
	private TextView accountBalance;
	

	public Fra_Details(DatabaseHandler pdbHandler)
	{
		dbHandler = pdbHandler;
		Log.e("Konstruktor Fra_Details", "test");
	}
	
	

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
			Log.e("onCreateView", "test");
	        View view = inflater.inflate(R.layout.la_ta_details,container,false);
	        s = (Spinner)view.findViewById(R.id.spChangeAccount);
	        detailsAdpa = new DetailListAdapter(this.getActivity(), new ArrayList<MoneyAmount>());
	        Log.e("Fragment", "Adapter: "+detailsAdpa.getCount());
	        ListView temp = (ListView)view.findViewById(R.id.listView1);
	        accountBalance = (TextView)view.findViewById(R.id.tvAccountBalance);
	        temp.setAdapter(detailsAdpa);
	        adapter = new AccountListAdapter(this.getActivity(),dbHandler.getAllAccounts());
	        s.setAdapter(adapter);
	        createEvent();
			return view;
	        
	    }

	
	 
	
	public void createEvent()
	{			 
		 s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
			        Log.e("SELECTED", ((Account)adapter.getItem(i)).getName() );
			        mData= dbHandler.getAllMoneyAmounts(((Account)adapter.getItem(i)).getId());
			        detailsAdpa.changeData(mData);
			        Log.e("SUM", "Summer: "+dbHandler.sumMoneyAmount(((Account)adapter.getItem(i)).getId()));
			        accountBalance.setText(""+dbHandler.sumMoneyAmount(((Account)adapter.getItem(i)).getId()));
			    } 

			    public void onNothingSelected(AdapterView<?> adapterView) {
			        return;
			    } 
			}); 
	}

}
